package client.heartbeat;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import client.TcpClient;

import util.ClientProtocol;
  
public class ServerHeartbeatListener {
	private TcpClient tc;
	private ScheduledExecutorService es = null;
	
	public ServerHeartbeatListener(TcpClient tc) {
		this.tc = tc;
	}
	
	public void start() {
		es = Executors.newSingleThreadScheduledExecutor();
		es.scheduleAtFixedRate(new TimeKeeper(), ClientProtocol.WAITGAP, ClientProtocol.WAITGAP, TimeUnit.MILLISECONDS);
	}
	
	private class TimeKeeper implements Runnable {
		@Override
		public void run() {
			if(!tc.getClient().isAlive()) {
				tc.disconnect();
				es.shutdownNow();
			}
			//tc.getClient().setAlive(false);
		}
	}

 }